package light.head.controller.oa;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Parm;
import light.head.constant.Url;
import light.head.dao.oa.WorkplanDAO;
import light.head.dao.sys.OrganDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.ExcelUtils;
import light.head.model.Workplan;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping(value = Url.C_WORKPLAN)
public class WorkplanController extends SupporterController{
	
	private final static String fileName = "工作计划统计.xls";
	private WorkplanDAO wpDAO;
	private OrganDAO organDAO;
	
	@RequestMapping(Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.WORKPLAN_INIT);
	}
	
	@RequestMapping(value = Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.NAME, Parm.TIME1, Parm.DELETED, Parm.TIME2,Parm.CU,Parm.OC,Parm.SUC);
		if (wpDAO.findByMap1(organDAO.getSonCodeList(cu_organCode(request)), map, pb) == RESULT.SUCCESS)
			request.setAttribute(Parm.PAGE_BEAN, pb);
		return new ModelAndView(Url.WORKPLAN_LIST);
	}

	

	
	/**
	 * 查询公开人
	 */
	@RequestMapping(Url.AID_INIT)
	public ModelAndView aidUserInit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String a = request.getParameter(Parm.A);
		request.setAttribute(Parm.A, a);
		return new ModelAndView(Url.WPAID_INIT);
	}

	@RequestMapping(Url.AID_LIST)
	public ModelAndView aidUserList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pageBean = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.REAL_NAME, Parm.SUC, Parm.OC);
		if (RESULT.SUCCESS == wpDAO.findSubUsers(organDAO.getSonCodeList(cu_organCode(request)), map, pageBean)) {
			request.setAttribute(Parm.PAGE_BEAN, pageBean);
		}
		return new ModelAndView(Url.WPAID_LIST);
	}
	
	// 导出
	@RequestMapping(Url.EXCEL_EXPORT)
	public void exclExport(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, String> map = createQueryMapByParmName(request, Parm.NAME, Parm.TIME1, Parm.DELETED, Parm.TIME2,Parm.CU,Parm.OC,Parm.SUC);
		map.put(Parm.CU, cu_code(request).toString());
		map.put(Parm.OC, cu_organCode(request).toString());
		List<Workplan> list = wpDAO.findByMap1(organDAO.getSonCodeList(cu_organCode(request)), map);
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("UTF-8"), "ISO-8859-1"));
		ExcelUtils.writeExcel(list, "工作计划统计", response.getOutputStream(), null, null);
	}
	@Autowired
	public void setWpDAO(WorkplanDAO wpDAO) {
		this.wpDAO = wpDAO;
	}
	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}

}
